package com.cw.base.common.dispatch.task.facade.ref;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.cw.base.common.model.dto.response.CommonResponse;
import com.cw.lang.common.exception.BizException;

/**
 * 定义facade的抽象模板<br>
 * Created on 15/10/19.
 *
 * @author zhangcheng
 */
public abstract class AbstractCompensateTaskFacade<K> {

    CommonResponse facadeManger(@NotNull @Valid K request) {
        CommonResponse commonResponse = CommonResponse.builder()
            .build();
        try {
            commonResponse = this.normal(request);
        } catch (Throwable throwable) {
            if (throwable instanceof BizException) {
                BizException bizException = (BizException) throwable;
                commonResponse.setErrorMessage(bizException.getErrorMessage());
                commonResponse.setErrorCode(bizException.getErrorCode());
            }
            throwable.printStackTrace();
        }
        return commonResponse;
    }

    /**
     * 添加到补偿队列
     *
     * @param k 任务
     * @return 处理结果
     */
    protected abstract CommonResponse normal(K k);
}
